package singleton;

/**
 * @author : DuJiabao
 * @Date : 2021/3/1 17:34
 * @Project : design-pattern
 * @File : null.java
 * @Desc : 双重检查
 */
public class Singleton4 {
  // 静态对象，volatile保证修改之后，立刻保存在内存中
  private static volatile Singleton4 instance;

  private Singleton4() {
  }

  // 返回对象
  public static Singleton4 getInstance() {
    // 双重检查
    if (instance == null) {
      synchronized (Singleton4.class) {
        if (instance == null) {
          instance = new Singleton4();
        }
      }
    }
    return instance;
  }
}
